/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jule.clases;

import java.util.ArrayList;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

/**
 * The methods in this class allow the JTree component to traverse
 * the file system tree and display the files and directories.
 **/
public class projectTreeModel implements TreeModel {
    // We specify the root when we create the model.

    protected project root;

    public projectTreeModel(project root) {
        this.root = root;
    }

    // The model knows how to return the root object of the tree
    public Object getRoot() {
        return root;
    }

    // Tell JTree whether an object in the tree is a leaf
    public boolean isLeaf(Object node) {
        try {
            field campo= (field)(node); 
            return true;
        } catch (Exception e) {
        }
        
        
        return false;
    // return ((File)node).isFile(); 


    }

    // Tell JTree how many children a node has
    public int getChildCount(Object parent) {
//    String[] children = ((File)parent).list();
//    if (children == null) return 0;
//    return children.length;
         try {
            ArrayList<table> tablas = ((project) parent).getTables();
            return tablas.size();
        } catch (Exception e) {
        }
         
         // ahora para los campos-fields
          try {
            ArrayList<field> campos = ((table) parent).getFields();
            return campos.size();
        } catch (Exception e) {
        }
        return 1;
    }

    // Fetch any numbered child of a node for the JTree.
    // Our model returns File objects for all nodes in the tree.  The
    // JTree displays these by calling the File.toString() method.
    public Object getChild(Object parent, int index) {
//    String[] children = ((File)parent).list();
//    if ((children == null) || (index >= children.length)) return null;
//    return new File((File) parent, children[index]);
//       fields[] children = (table)parent.getFields();
//        return children[index];

        // como tenemos dos estructuras tenemos que hacer...
        // una para las tablas y otra para los campos...
        try {
            ArrayList<table> tablas = ((project) parent).getTables();
            return tablas.get(index);
        } catch (Exception e) {
        }

        try {
            ArrayList<field> campos = ((table) parent).getFields();
            return campos.get(index);
        } catch (Exception e) {
        }
//      ArrayList<table> tablas = ((project)parent).getTables();
//      return tablas.get(index);
     return null;
    }

    // Figure out a child's position in its parent node.
    public int getIndexOfChild(Object parent, Object child) {
//    String[] children = ((File)parent).list();
//    if (children == null) return -1;
//    String childname = ((File)child).getName();
//    for(int i = 0; i < children.length; i++) {
//      if (childname.equals(children[i])) return i;
//    }
//    return -1;
        return -1;
    }

    // This method is invoked by the JTree only for editable trees.  
    // This TreeModel does not allow editing, so we do not implement 
    // this method.  The JTree editable property is false by default.
    public void valueForPathChanged(TreePath path, Object newvalue) {
    }

    // Since this is not an editable tree model, we never fire any events,
    // so we don't actually have to keep track of interested listeners
    public void addTreeModelListener(TreeModelListener l) {
    }

    public void removeTreeModelListener(TreeModelListener l) {
    }
}
